package com.micro;

import java.util.HashMap;
import java.util.Map;

import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.micro.communication.ApiService;
import com.micro.communication.ApiServiceInterface;
import com.micro.communication.ApiServiceListener;
import com.micro.constant.IGeneral;
import com.micro.data.AccountInfo;
import com.micro.service.CommonHandler;
import com.micro.service.JsonToData;
import com.sina.oauth.OAuthConstant;

public class RegisterSinaAccountActivity extends Activity implements
		ServiceConnection {

	private AccessToken accessToken;
	private ApiServiceInterface apiServiceInterface;
	private ApiServiceListener.Stub apiServiceListener = new ApiServiceListener.Stub() {

		@Override
		public void requestCompleted(String service, int type,
				String statusCode, String message) throws RemoteException {
			Log.d("request completed", "---------------------");
			setProgressBarIndeterminateVisibility(false);
			if(statusCode.equals("200")){
				AccountInfo account = JsonToData.getAccountInfo(message);
				account.setToken(accessToken.getToken());
				account.setTokenScret(accessToken.getTokenSecret());
				account.setContext(RegisterSinaAccountActivity.this);
				account.persist();
			};
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_sina);
		try {
			Uri uri = this.getIntent().getData();
			RequestToken requestToken = OAuthConstant.getInstance()
					.getRequestToken();
			accessToken = requestToken.getAccessToken(uri
					.getQueryParameter("oauth_verifier"));
			OAuthConstant.getInstance().setAccessToken(accessToken);
			Intent intent = new Intent(this, ApiService.class);
			bindService(intent, this, Context.BIND_AUTO_CREATE);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		apiServiceInterface = ApiServiceInterface.Stub.asInterface(service);
		registerToken(accessToken.getToken(), accessToken.getTokenSecret());
	}

	@Override
	protected void onStop() {
		super.onStop();
		unbindService(this);
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		apiServiceInterface = null;
	}

	private void registerToken(String accessToken, String tokenSecret) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("accessToken", accessToken);
		parameters.put("tokenSecret", tokenSecret);
		try {
			apiServiceInterface.request(IGeneral.SERVICE_NAME_SINA,
					CommonHandler.TYPE_VERIFY_USER, apiServiceListener,
					parameters);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

}
